package cn.wsn2018.tools.dao.impl;

import cn.wsn2018.tools.dao.LogsInfoDao;
import cn.wsn2018.tools.model.Log.ErrorLogInfo;
import cn.wsn2018.tools.model.Log.LogInfo;
import cn.wsn2018.tools.model.Log.LogsInfo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.logging.Logger;

/**
 * @author gmz.
 * @date 2018/12/14.
 * @time 15:09
 */
@Repository("logsInfoDao")
public class LogsInfoDaoImpl implements LogsInfoDao {
    private Logger logger = Logger.getLogger(LogsInfoDaoImpl.class.getName());

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询某操作员的所有日志信息,按时间降序排列
     * @param username
     * @return
     */
    @Override
    public List<LogsInfo> listLogsInfoByUser(String username,int pageNum,int pageSize){
        Query query = new Query();
        query.addCriteria(Criteria.where("username").is(username));
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<LogsInfo> logsInfoList = mongoTemplate.find(query,LogsInfo.class,"logsInfo");
        return logsInfoList;
    }

    /**
     * 实现分页,查询某一页的日志信息，并且该信息是针对所有操作员
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public  List<LogsInfo> listLogsByPageNum(int pageNum,int pageSize){
        Query query = new Query();
        //排序
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<LogsInfo> datas = mongoTemplate.find(query, LogsInfo.class,"logsInfo");
        return datas;
    }

    /**
     * 用于test，插入一条日志信息
     * @param logsInfo
     * @return
     */
    @Override
    public boolean saveLogsInfo(LogsInfo logsInfo){
        JSONObject result = JSONObject.fromObject(mongoTemplate.insert(logsInfo,"logsInfo"));
        String id = result.optString("id");
        if(id!=null&&!"".equals(id)) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 根据时间范围查询所有操作员的日志信息
     * @param fromTime
     * @param toTime
     * @return
     */
    @Override
    public List<LogsInfo> listLogsInfoByTime(int pageNum,int pageSize,String fromTime,String toTime){
        Query query = new Query();
        query.addCriteria(Criteria.where("createTime").gte(fromTime).lte(toTime));
        //排序
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<LogsInfo> datas = mongoTemplate.find(query, LogsInfo.class,"logsInfo");

        return datas;
    }

    /**
     * 根据时间范围、操作员，查询日志信息
     * @param username
     * @param fromTime
     * @param toTime
     * @return
     */
    @Override
    public List<LogsInfo> listLogsInfoByTimeAndName(String username,int pageNum,int pageSize,String fromTime,String toTime){
        Query query = new Query();
        //添加查询条件
        query.addCriteria(Criteria.where("username").is(username));
        query.addCriteria(Criteria.where("createTime").gte(fromTime).lte(toTime));
        //排序
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<LogsInfo> datas = mongoTemplate.find(query, LogsInfo.class,"logsInfo");

        return datas;
    }

    @Override
    public List<LogsInfo> listLogsInfoByCond(String username, int pageNum, int pageSize, String fromTime, String toTime) {
        Query query = new Query();
        //添加查询条件
        if(!username.isEmpty()){
            query.addCriteria(Criteria.where("username").is(username));
        }
        if(!fromTime.isEmpty()&&!toTime.isEmpty()){
            query.addCriteria(Criteria.where("createTime").gte(fromTime).lte(toTime));
        }else if (!fromTime.isEmpty()){
            query.addCriteria(Criteria.where("createTime").gte(fromTime));
        }else if (!toTime.isEmpty()){
            query.addCriteria(Criteria.where("createTime").lte(toTime));
        }
        //排序
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);
        List<LogsInfo> datas = mongoTemplate.find(query, LogsInfo.class,"logsInfo");

        return datas;
    }





    /**
     * @Description: 保存日志
     * @Param: [logInfo, collectionName]
     * @return: boolean
     * @Author: SongDaisong
     * @Date: 21:01
     */
    //保存和分页的万能dao
    @Override
    public boolean saveLogInfo(LogInfo logInfo, String collectionName) {

        String id = mongoTemplate.insert(logInfo, collectionName).getId();
        if(id!=null&&!"".equals(id)) {
            return true;
        }
        else {
            return false;
        }
    }


    /**
     * @Description:className:指定日志类
     * @Param: [className, pageNum, pageSize, fromTime, toTime]
     * @return: java.util.List<cn.wsn2018.tools.model.Log.LogsInfo>
     * @Author: SongDaisong
     * @Date: 21:01
     */
    @Override
    public List<LogsInfo> listLogInfoByCond(String username,int pageNum, int pageSize, String fromTime, String toTime,Class className,String collectionName) {

        Query query = new Query();
        //添加查询条件
        if(!username.isEmpty()){
            query.addCriteria(Criteria.where("userName").is(username));
        }
        //时间范围
        if((!fromTime.isEmpty()) && (!toTime.isEmpty()) ){
            query.addCriteria(Criteria.where("createTime").gte(fromTime).lte(toTime));
        }else if(!fromTime.isEmpty()){
            query.addCriteria(Criteria.where("createTime").gte(fromTime));
        }else if (!toTime.isEmpty()){
            query.addCriteria(Criteria.where("createTime").lte(toTime));
        }

        //排序
        query.with(new Sort(Sort.Direction.DESC,"createTime"));
        query.skip((pageNum-1)*pageSize).limit(pageSize);

        List<LogsInfo> logInfoList = mongoTemplate.find(query, className, collectionName);
        return logInfoList;
    }


}
